#!/bin/bash
echo "=== 启动统一 DevOps 平台开发环境 ==="

# 创建必要的目录
mkdir -p /d/vc/devops-platform/data
mkdir -p /d/vc/devops-platform/logs
mkdir -p /d/vc/repositories

echo "1. 检查依赖..."
if ! command -v cmake &> /dev/null; then
    echo "❌ CMake 未安装，请先安装: pacman -S cmake"
    exit 1
fi

if ! command -v sqlite3 &> /dev/null; then
    echo "❌ SQLite3 未安装，请先安装: pacman -S sqlite3"
    exit 1
fi

echo "2. 初始化数据库..."
cd /d/vc/devops-platform
if [ -f "./build/devops-platform" ]; then
    ./build/devops-platform --init-db &
    SERVER_PID=$!
    echo "🔧 服务器进程ID: $SERVER_PID"
else
    echo "⚠️  可执行文件不存在，请先运行构建脚本"
    echo "💡 执行: /d/vc/devops-platform/tools/scripts/build-platform.sh"
fi

echo "3. 启动调试仪表板..."
cd /d/vc/devops-platform/tools/debug
if command -v python3 &> /dev/null; then
    python3 -m http.server 3000 > /d/vc/devops-platform/logs/debug-dashboard.log 2>&1 &
    DASHBOARD_PID=$!
    echo "📊 调试面板进程ID: $DASHBOARD_PID"
else
    echo "⚠️  Python3 未安装，调试面板无法启动"
fi

echo "4. 检查服务状态..."
sleep 3
if curl -s http://localhost:8080/auth/validate | grep -q "valid"; then
    echo "✅ OAuth2 服务运行正常"
else
    echo "❌ OAuth2 服务异常或未启动"
fi

echo ""
echo "=== 开发环境就绪 ==="
echo "🌐 API 服务: http://localhost:8080"
echo "📊 调试面板: http://localhost:3000"
echo "📁 日志目录: /d/vc/devops-platform/logs/"
echo "💾 数据目录: /d/vc/devops-platform/data/"
echo ""
echo "🛑 停止服务请运行: /d/vc/devops-platform/tools/scripts/stop-dev.sh"
